package net.babeuf.form.utils 
{
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class TextFieldUtils
	{		
		public static function isValidEmail( email : String ) : Boolean
		{
		//	var emailExpression:RegExp = /^([a-zA-Z0-9][\w-\.]+)@(([\w-]+\.)+)([a-zA-Z]{2,6})$/gi;
			var emailExpression:RegExp = /^([\w-\.]+)@(([\w-]+\.)+)([a-zA-Z]{2,6})$/gi;
			return emailExpression.test(email);
		}
				
		public static function isValidPostal( postal : String ) : Boolean 
		{			
			return (postal.length == 5) && Number(postal) > 0 ;
		}
		
		public static function isValidPhone( phone : String, locale : String = "fr"  ) : Boolean
		{
			switch (locale )
			{
				case Locale.FR :
					if ( phone.substr(0, 1) != "0" ) return false ; 			
					return (phone.length == 10) && Number(phone) > 0 ;
				break ; 
				case Locale.US :
					return (phone.length == 10) ; 				
				break ;
				default : 
					return true ; 
				break ; 
			}		
		}
		
		
		
		
		public static function isValidMobilePhone( phone : String, locale : String = "fr" ) : Boolean
		{
			switch (locale )
			{
				case Locale.FR :
					if ( phone.substr(0, 2) != "06"  && phone.substr(0, 2) != "07" ) return false ; 			
					return (phone.length == 10) && Number(phone) > 0 ;
				break ; 
				case Locale.US :
					return (phone.length == 10) ; 				
				break ;
				default : 
					return true ; 
				break ; 
			}					
		}
		
		public static function formatPostal( postal : String ) : String 
		{
			var p : String = Number(postal).toString() ;
			while ( p.length < 5 )  p = "0" + p ;
			return p  ;			
		}
		
		public static function trim( value :String ) : String
		{
		    var begin:Number = 0;
		    var end:Number = -1;
			
			for (var l:Number = 0; l < value.length; l++)
		    {
			   if (value.charAt(l)!=" ") { begin=l; break; }
		    }
		    for (l = value.length - 1; l >= 0; l--)
		    {
			   if (value.charAt(l)!=" ") { end=l; break; }
		    }
		    return value.substr(begin, value.length - begin - (value.length - end - 1));
		}
		
		public static function cleanAccents( input : String ) :  String
		{
			var o : Object = {"\u0160":"S","\u0161":"s","\u0110":"Dj","\u0111":"dj","\u017d":"Z","\u017e":"z","\u010c":"C","\u010d":"c","\u0106":"C","\u0107":"c","\u00c0":"A","\u00c1":"A","\u00c2":"A","\u00c3":"A","\u00c4":"A","\u00c5":"A","\u00c6":"A","\u00c7":"C","\u00c8":"E","\u00c9":"E","\u00ca":"E","\u00cb":"E","\u00cc":"I","\u00cd":"I","\u00ce":"I","\u00cf":"I","\u00d1":"N","\u00d2":"O","\u00d3":"O","\u00d4":"O","\u00d5":"O","\u00d6":"O","\u00d8":"O","\u00d9":"U","\u00da":"U","\u00db":"U","\u00dc":"U","\u00dd":"Y","\u00de":"B","\u00df":"Ss","\u00e0":"a","\u00e1":"a","\u00e2":"a","\u00e3":"a","\u00e4":"a","\u00e5":"a","\u00e6":"a","\u00e7":"c","\u00e8":"e","\u00e9":"e","\u00ea":"e","\u00eb":"e","\u00ec":"i","\u00ed":"i","\u00ee":"i","\u00ef":"i","\u00f0":"o","\u00f1":"n","\u00f2":"o","\u00f3":"o","\u00f4":"o","\u00f5":"o","\u00f6":"o","\u00f8":"o","\u00f9":"u","\u00fa":"u","\u00fb":"u","\u00fd":"y","\u00fe":"b","\u00ff":"y","\u0154":"R","\u0155":"r","\u0 0fc":"u","\u0178":"Y","\u0101":"a","\u0103":"a","\u0105":"a","\u0100":"a","\u0102":"a","\u0104":"a","\u0113":"e","\u0115":"e","\u0117":"e","\u0119":"e","\u011b":"e","\u0112":"e","\u0114":"e","\u0116":"e","\u0118":"e","\u011a":"e","\u0129":"i","\u012b":"i","\u012d":"i","\u0128":"i","\u012a":"i","\u012c":"i","\u014d":"o","\u014f":"o","\u0151":"o","\u014c":"o","\u014e":"o","\u0150":"o","\u0169":"u","\u016b":"u","\u016d":"u","\u016f":"u","\u0168":"u","\u016a":"u","\u016c":"u","\u016e":"u"}
			for ( var i : String in o ) input = input.split(i).join(o[i]) ; 
			return input.toLowerCase() ;			
		}
		
		
		public static  function levenshteinDistance( s1 : String, s2 : String ) : int
		{
			var m:int=s1.length;
			var n:int=s2.length;
			var matrix:Array=new Array();
			var line:Array;
			var i:int;
			var j:int;
			for (i=0;i<=m;i++)
			{
				line=new Array();
				for (j=0;j<=n;j++)
				{
					if (i!=0)line.push(0)
					else line.push(j);
				}
				line[0]=i
				matrix.push(line);
			}
			var cost:int; 
			for (i=1;i<=m;i++)
			for (j=1;j<=n;j++)
			{
				if (s1.charAt(i-1)==s2.charAt(j-1)) cost=0
				else cost=1;
				matrix[i][j]=Math.min(matrix[i-1][j]+1,matrix[i][j-1]+1,matrix[i-1][j-1]+cost);
			}
			return matrix[m][n]; 
		}
		
		
	}

}